﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using SDT.Lib.Common;

namespace SDT.Model
{
    public class UserManager
    {
        public static MembershipUser GetMember(string userName)
        {
            try
            {
                SDT.Lib.MvcMembership.AspNetMembershipProviderWrapper a = new SDT.Lib.MvcMembership.AspNetMembershipProviderWrapper(System.Web.Security.Membership.Provider);
                MembershipUser u = a.Get(userName);
                return u;
            }
            catch (Exception exp)
            {
                throw exp;
            }
        }

        public static MembershipUser GetMember(object userNameorKey, bool iskey)
        {
            try
            {
                SDT.Lib.MvcMembership.AspNetMembershipProviderWrapper a = new SDT.Lib.MvcMembership.AspNetMembershipProviderWrapper(System.Web.Security.Membership.Provider);
                MembershipUser u = iskey ? a.Get(userNameorKey) : GetMember(userNameorKey.ToString());
                if (u == null) return GetMember("admin");
                return u;
            }
            catch (Exception exp)
            {
                throw exp;
            }
        }
        public static MembershipUser GetUser(object userNameorKey, bool iskey)
        {
            try
            {
                SDT.Lib.MvcMembership.AspNetMembershipProviderWrapper a = new SDT.Lib.MvcMembership.AspNetMembershipProviderWrapper(System.Web.Security.Membership.Provider);
                MembershipUser u = iskey ? a.Get(userNameorKey) : GetMember(userNameorKey.ToString());
                if (u == null) return null;
                return u;
            }
            catch (Exception exp)
            {
                throw exp;
            }
        }
        public static Guid GetProviderUserKey(string userName)
        {
            try
            {
                SDT.Lib.MvcMembership.AspNetMembershipProviderWrapper a = new SDT.Lib.MvcMembership.AspNetMembershipProviderWrapper(System.Web.Security.Membership.Provider);
                MembershipUser u = a.Get(userName);
                return (Guid)u.ProviderUserKey;
            }
            catch (Exception exp)
            {
                throw exp;
            }
        }
        public static bool CheckRole(string userName, string role)
        {
            try
            {
                SDT.Lib.MvcMembership.AspNetMembershipProviderWrapper a = new SDT.Lib.MvcMembership.AspNetMembershipProviderWrapper(System.Web.Security.Membership.Provider);
                MembershipUser u = a.Get(userName);
                SDT.Lib.MvcMembership.AspNetRoleProviderWrapper r = new SDT.Lib.MvcMembership.AspNetRoleProviderWrapper(Roles.Provider);
                if (r != null && u != null)
                {
                    if (r.IsInRole(u, RoleSystem.Administrator)) return true;
                    else
                        return r.IsInRole(u, role);
                }
                else return false;
            }
            catch (Exception exp)
            {
                throw exp;
            }
        }
        public static bool CheckRole(string role)
        {
            string userName = HttpContext.Current.User.Identity.Name;
            return CheckRole(userName, role);
        }
        public static Guid GetUserId()
        {
            MembershipUser myObject = Membership.GetUser();
            return new Guid(myObject.ProviderUserKey.ToString());
        }
        public static MembershipCreateStatus CreateUser(string userName, string password, string email)
        {
            MembershipCreateStatus status;
            Membership.Provider.CreateUser(userName, password, email, null, null, true, null, out status);
            return status;

        }
        public static bool CheckByEmail(string email)
        {
            try
            {
                SDT.Lib.MvcMembership.AspNetMembershipProviderWrapper a = new SDT.Lib.MvcMembership.AspNetMembershipProviderWrapper(System.Web.Security.Membership.Provider);
                var l = a.FindByEmail(email, 0, 10);
                return l.Count > 0;
            }
            catch (Exception exp)
            {
                throw exp;
            }
        }
        public static MembershipUser GetByEmail(string email)
        {
            try
            {
                SDT.Lib.MvcMembership.AspNetMembershipProviderWrapper a = new SDT.Lib.MvcMembership.AspNetMembershipProviderWrapper(System.Web.Security.Membership.Provider);
                var l = a.FindByEmail(email, 0, 10);
                return l.Count > 0 ? l.FirstOrDefault() : null;
            }
            catch (Exception exp)
            {
                throw exp;
            }
        }
    }
}
